<?php
/**
 * Created by PhpStorm.
 * User: 17208
 * Date: 2020/10/13
 * Time: 17:21
 */

namespace extend;


class Response
{
    /** @var int 错误状态码 */
    public static $OK = 0;
    public static $ERROR = 999;
    public static $API_LOGIN_ERROR = 998;

    /** 正常信息输出
     * @param array|String  $data
     * @param string  $message
     * @return object
     */
    public static function res_ok($data,$message="成功"){
        return self::res(self::$OK,$data,$message);
    }
    /** 错误信息输出
     * @param string  $message
     * @param array  $data
     * @return object
     */
    public static function res_error($message,$data=[]){
        return self::res(self::$ERROR,$data,$message);
    }
    /** 异常信息输出
     * @param \Exception  $e
     * @param array  $data
     * @return object
     */
    public static function res_exp($e,$data=[]){
        $code = $e->getCode()?$e->getCode():self::$ERROR;
        return self::res($code,$data,$e->getMessage());
    }
    /** 信息输出
     * @param string  $code
     * @param array|string  $data
     * @param string  $message
     * @return object
     */
    public static function res($code,$data,$message){
        echo json_encode([
            'code'=>$code,
            'data'=>$data,
            'message'=>$message,
        ],JSON_UNESCAPED_UNICODE);
        exit();
    }
}